trigger setAchieveOwner on Achieve__c (before insert) {
    list<id> idl=new list<id>();
    list<hospital__c> hosl=new list<hospital__c>();
    
    for(achieve__c ach:trigger.new){
        idl.add(ach.Hospital_Name__c);
    }
    hosl=[select id,ownerid from hospital__c where id in:idl];
    
    for(achieve__c ach:trigger.new){
        for(hospital__c hos:hosl){
            if(hos.id==ach.hospital_name__c){
                ach.ownerid=hos.ownerid;
            }
        }
    }
    
       Date Insertdate = date.newinstance(Integer.valueof(trigger.new[0].Year__c),Integer.valueof(trigger.new[0].Month__c),1); 
       date InsertdateNextmonth = Insertdate.addMonths(1) - 1; 
	   List<AggregateResult> CElist = [Select sum(arrived_pclass__c) arr,sum(firstpurchasevalue__c)fir,OrganigerHospital__c org
                                            from ConsumerEducation__c where Type__c = 'PClass'
                                            and  Date__c >=: Insertdate and Date__c <=: InsertdateNextmonth Group by OrganigerHospital__c];                                    
       Map<id,AggregateResult> CEmap = new Map<id,AggregateResult>();
       for(AggregateResult ce : CElist)
       {
       	CEmap.put(String.valueof(ce.get('org')),ce);
       }
       for(achieve__c ach : trigger.new){ 
   		if(CEmap.containsKey(ach.hospital_name__c))
   		{
   			ach.P_class_cover_actual_calc__c = (double)CEmap.get(ach.hospital_name__c).get('arr');
   			ach.X1st_Purchase_actual_calc__c = (double)CEmap.get(ach.hospital_name__c).get('fir');
   		}
       		
        }
}